package br.com.felinelayer.server.application;

import br.com.felinelayer.common.Message;
import br.com.felinelayer.common.Protocol;
import java.util.ArrayList;

/**
 *
 * @author Vanius Roberto Bittencourt
 */
public class FrameServer extends ComponentServer<FrameServer> {
    
    private int currentRow = 0;
    private int currentCol = 0;
    private ArrayList<ComponentServer> components = new ArrayList<>();
    
//    public FrameServer(Application application) {
//        
//    }

    public FrameServer setCurrentRow( int currentRow ) {
        this.currentRow = currentRow;
        return this;
    }
    
    public int getCurrentRow() {
        return currentRow;
    }
    
    public FrameServer setCurrentCol( int currentCol ) {
        this.currentCol = currentCol;
        return this;
    }
    
    public int getCurrentCol() {
        return currentCol;
    }
    
    public FrameServer addRow() {
        ++currentRow;
        currentCol = 0;
        return this;
    }
    
    public <T extends ComponentServer> T addComponent( T component ) {
        components.add( component );
        component.setFrame( this );
        component.setRow( currentRow );
        component.setCol( currentCol );
        return component;
    }
    
    public ButtonServer addButton() {
        return addComponent(  getApplication().addButton(this) );
    }
    
    public ComboBoxServer addComboBox() {
        return addComponent( getApplication().addComboBox(this) );
    }
    
    public EditServer addEdit() {
        return addComponent( getApplication().addEdit(this) );
    }
    
    public GridServer addGrid() {
        return addComponent( getApplication().addGrid(this) );
    }
    
    public LabelServer addLabel() {
        return addComponent( getApplication().addLabel(this) );
    }

    public LabelServer addLabel( String text ) {
        return addComponent( getApplication().addLabel(this) ).setText( text );
    }
    
    public void show() {
        Message m = new Message( Protocol.MSG_NAME_METHOD );
        m.setValueByName( Protocol.METHOD_NAME, Protocol.METHOD_FRAME_SHOW );
        sendMessage( m );
    }
    
    public void close() {
        for ( ComponentServer c : components ) {
            c.clearListeners();
        }
        Message m = new Message( Protocol.MSG_NAME_METHOD );
        m.setValueByName( Protocol.METHOD_NAME, Protocol.METHOD_FRAME_CLOSE );
        inquireMessage( m );
    }
    
    @Override
    public String getComponentClassName() {
        return Protocol.COMPONENT_FRAME;
    }

}
